//app.js
var code = "";
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    wx.login({
      success: res => {
        code = res.code;
        this.login(code)
        this.loadNewCoupon()
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })

  },
  login: function (code) {
    let that = this

    // 获取用户信息
    wx.getSetting({

      success: res => {
        wx.request({
          url: 'https://small.qiongyazuche.com/sunday/small/member/loginWithCode',
          method: 'post',
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          data: {
            'code': code
          },

          success: function (result) {

            that.globalData.userInfo = result.data.result

            if (that.userInfoReadyCallback) {
              that.userInfoReadyCallback(result.data.result)
            }
          }
        })
      }
    })
  },
  globalData: {
    userInfo: null,
    baseURL:'https://small.qiongyazuche.com/sunday/small/',
    couponValue:0
  },
  synPostData: function (url, data) {
    var that = this;
    return new Promise(function (resolve, reject) {
      wx.request({
        url: that.globalData.baseURL + url,
        data: data,
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          resolve(res);
        },
        fail: function (res) {
          reject(res);
        },
      })


    });
  },
  postData: function (url, data){
    var that = this;
    var result='heh';
    wx.request({
      url: that.globalData.baseURL + url,
      data: data,
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        result=res.data
      },
      fail: function (res) {
        result = res.data
      },
    })
   
  },
  //加载新人券
  loadNewCoupon:function(){
    var that = this
    wx.request({
      url: 'https://small.qiongyazuche.com/ace/system/dictionary/getDictionaryByKey',
      data: { 'key':'newCoupons'},
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        that.globalData.couponValue = parseFloat(res.data[0].value).toFixed(2)
      },
      fail: function (res) {

      },
    })
  }
  
})



